<html>
<head>
<h1>View Transcript</h1>
</head>

<body>
<form action="viewTranscript.php" method="GET">
Student Id: <input type='text' name = 'sid' /> <br/>

<input type='submit' name = 'view' value='View'/>
</form>
</body>

<?php
	require "config.php";
	$grade_value = array("A" => 4.00,
						"A-" => 3.70, abc
						"B+" => 3.33,
						"B" => 3.00,
						"B-" => 2.70,
						"C+" => 2.30,
						"C" => 2.00,
						"C-" => 1.70,
						"D+" => 1.30,
						"D" => 1.00,
						"D-" => 0.70 );
	define("CREDITS_PER_COURSE", 3);
	
	$studentid = isset($_GET['sid']);
	$con = mysql_connect("localhost",$db_user ,$db_pass);
	//if an error was encountered, the script exits with an error message
	if (!$con){
		die('Could not connect: ' . mysql_error());
    }
	//after successfully connecting with the database server, select the database with which you want to communicate
	mysql_select_db($db_name, $con);
	
	//code to print the transcript-with the current term taken from student table and course table as one variable
	
	$sql_get = "select * from coursesection where (studentId = '$studentid')" ;
	$result = mysql_query($sql_get);
	$calcugpa = calculate_gpa($studentid); //function to get the GPA
	echo $calcugpa;
	
	/*while ($row = mysql_fetch_array($result)){
		$coursesection = $row["courseSection"];
		$courseId=$row["courseId"];
		$grade = $row["grade"];
		$calcugpa = calculate_gpa($studentid);
		echo $coursesection;
		echo $calcugpa;
		//break;
	}*/
	//mysql_close($con);
	
	//function to calculate GPA
	function calculate_gpa($val){
		global $grade_value;
		require 'config.php';
		$con = mysql_connect("localhost",$db_user ,$db_pass);
		if (!$con){
				die('Could not connect: ' . mysql_error());
		}
		mysql_select_db($db_name, $con);
		//calculating for courses enrolled in current term by student
		/*$sql_get = "select * from coursesection AS CS,course AS C,student where (C.courseId=CS.courseId) AND 
					(CS.studentId=student.studentId ) AND (student.term=course.currentTerm) AND (studentId = '$val')" ;*///for the current term calculate GPA
		$sql_get = "select * from coursesection where (studentId = '$val')" ;//for the current term calculate GPA
		$result = mysql_query($sql_get);
		$totalCredits = 0;
		$totalnumcourses = 0;
		while ($row = mysql_fetch_array($result)){
			$grade = $row["grade"];
			if ( array_key_exists($grade, $grade_value)) {
				$totalCredits += $grade_value[$grade] * CREDITS_PER_COURSE;
				$totalnumcourses += CREDITS_PER_COURSE;
			} 
		}
		$gpa = 0;
		if($totalnumcourses > 0) {
			$gpa = $totalCredits/$totalnumcourses; // you may want to limit floating decimals
			 round($gpa, 3);
		}
 		return $gpa;
	}
	
?>
<br />
<a href="student.php">Home</a>
</html>

